package com.alliky.core.loader;

import android.content.Context;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatDialog;

import com.alliky.core.R;
import com.alliky.core.utils.DimenUtil;
import com.alliky.core.utils.StringUtils;
import com.wang.avi.AVLoadingIndicatorView;

import java.util.ArrayList;

/**
 * Author wxianing
 * date 2018/6/26
 */
public class Loader {

    private static final int LOADER_SIZE_SCALE = 8;
    private static final int LOADER_OFFSET_SCALE = 10;

    private static final ArrayList<AppCompatDialog> LOADERS = new ArrayList<>();

    private static final String DEFAULT_LOADER = LoaderStyle.LineSpinFadeLoaderIndicator.name();

    private static final String MESSAGE = "正在加载";

    public static void showLoading(Context context, String type, String message, boolean cancelable) {

        final AppCompatDialog dialog = new AppCompatDialog(context, R.style.dialog_loading);

//        final AVLoadingIndicatorView avLoadingIndicatorView = LoaderCreator.create(type, context);

        dialog.setContentView(R.layout.loading_layout);

        TextView tvMessage = dialog.findViewById(R.id.tv_message);

        if (StringUtils.isNotBlank(message)) {
            tvMessage.setText(message);
        }

//        int deviceWidth = DimenUtil.getScreenWidth();
//        int deviceHeight = DimenUtil.getScreenHeight();
//        final Window dialogWindow = dialog.getWindow();
//        if (dialogWindow != null) {
//            final WindowManager.LayoutParams lp = dialogWindow.getAttributes();
//            lp.width = deviceWidth / LOADER_SIZE_SCALE;
//            lp.height = deviceHeight / LOADER_SIZE_SCALE;
//            lp.height = lp.height + deviceHeight / LOADER_OFFSET_SCALE;
//            lp.gravity = Gravity.CENTER;
//        }
        dialog.setCanceledOnTouchOutside(cancelable);
        LOADERS.add(dialog);
        dialog.show();
    }

    public static void showLoading(Context context) {
        showLoading(context, DEFAULT_LOADER, MESSAGE, true);
    }

    public static void showLoading(Context context, boolean cancelable) {
        showLoading(context, DEFAULT_LOADER, MESSAGE, cancelable);
    }

    public static void showLoading(Context context, Enum<LoaderStyle> type) {
        showLoading(context, type.name(), MESSAGE, true);
    }

    public static void showLoading(Context context, Enum<LoaderStyle> type, boolean cancelable) {
        showLoading(context, type.name(), MESSAGE, cancelable);
    }

    public static void showLoading(Context context, Enum<LoaderStyle> type, String message, boolean cancelable) {
        showLoading(context, type.name(), message, cancelable);
    }

    public static void stopLoading() {
        for (AppCompatDialog dialog : LOADERS) {
            if (dialog != null) {
                if (dialog.isShowing()) {
                    dialog.dismiss();
                }
            }
        }
    }
}
